#! /bin/bash
# set -ex
ORDERER_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/ordererOrganizations/example.com/orderers/orderer.example.com/msp/tlscacerts/tlsca.example.com-cert.pem

channelName=mychannel
chaincodeName=entity1cc3
chaincodePath=github.com/chaincode/entity

CORE_PEER_LOCALMSPID="Org1MSP" 
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt 
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp 
# for (( i = 0; i < 2; i++ )); do
#     CORE_PEER_ADDRESS=peer${i}.org1.example.com:7051
#     peer chaincode install -n ${chaincodeName} -v 1.0 -p ${chaincodePath}
# done
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
peer chaincode install -n ${chaincodeName} -v 1.0 -p ${chaincodePath}

CORE_PEER_ADDRESS=peer1.org1.example.com:8051
peer chaincode install -n ${chaincodeName} -v 1.0 -p ${chaincodePath}


CORE_PEER_LOCALMSPID="Org2MSP" 
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt 
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp 
# for (( i = 0; i < 2; i++ )); do
#     CORE_PEER_ADDRESS=peer${i}.org2.example.com:7051
#     peer chaincode install -n ${chaincodeName} -v 1.0 -p ${chaincodePath}
# done
CORE_PEER_ADDRESS=peer0.org2.example.com:9051
peer chaincode install -n ${chaincodeName} -v 1.0 -p ${chaincodePath}

CORE_PEER_ADDRESS=peer1.org2.example.com:10051
peer chaincode install -n ${chaincodeName} -v 1.0 -p ${chaincodePath}

 
CORE_PEER_LOCALMSPID="Org1MSP" 
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt 
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp 
CORE_PEER_ADDRESS=peer0.org1.example.com:7051

peer chaincode instantiate -o orderer.example.com:7050 --tls true --cafile $ORDERER_CA -C ${channelName} -n ${chaincodeName} -v 1.0 -c '{"Args":["init"]}' -P "OR ('Org1MSP.member','Org2MSP.member')"

sleep 10

# for (( i = 0; i < 2; i++ )); do
#     CORE_PEER_ADDRESS=peer${i}.org1.example.com:7051
#     peer chaincode query -C ${channelName} -n ${chaincodeName} -c '{"Args":["GetEntity","a"]}'
# done
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
peer chaincode query -C ${channelName} -n ${chaincodeName} -c '{"Args":["GetEntity","a"]}'
CORE_PEER_ADDRESS=peer1.org1.example.com:8051
peer chaincode query -C ${channelName} -n ${chaincodeName} -c '{"Args":["GetEntity","a"]}'


CORE_PEER_LOCALMSPID="Org2MSP" 
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt 
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp 
# for (( i = 0; i < 2; i++ )); do
#     CORE_PEER_ADDRESS=peer${i}.org2.example.com:7051
#     peer chaincode query -C ${channelName} -n ${chaincodeName} -c '{"Args":["GetEntity","a"]}'
# done
CORE_PEER_ADDRESS=peer0.org2.example.com:9051
peer chaincode query -C ${channelName} -n ${chaincodeName} -c '{"Args":["GetEntity","a"]}'
CORE_PEER_ADDRESS=peer1.org2.example.com:10051
peer chaincode query -C ${channelName} -n ${chaincodeName} -c '{"Args":["GetEntity","a"]}'



sleep 3
OrgA_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/msp/tlscacerts/tlsca.org1.example.com-cert.pem
OrgB_CA=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/msp/tlscacerts/tlsca.org2.example.com-cert.pem
peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile  $ORDERER_CA  -C ${channelName} -n ${chaincodeName} -c '{"Args":["CreateEntity","123456789","3","createTime"]}' --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles  $OrgA_CA --peerAddresses peer1.org1.example.com:8051 --tlsRootCertFiles  $OrgA_CA --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles  $OrgB_CA --peerAddresses peer1.org2.example.com:10051 --tlsRootCertFiles  $OrgB_CA 


sleep 20

CORE_PEER_LOCALMSPID="Org1MSP" 
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/peers/peer0.org1.example.com/tls/ca.crt 
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp 
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
# for (( i = 0; i < 2; i++ )); do
#     CORE_PEER_ADDRESS=peer${i}.org1.example.com:7051
#     peer chaincode query -C ${channelName} -n ${chaincodeName} -c '{"Args":["GetEntity","b"]}'
# done
CORE_PEER_ADDRESS=peer0.org1.example.com:7051
peer chaincode query -C ${channelName} -n ${chaincodeName} -c '{"Args":["GetEntity","123456789"]}'
CORE_PEER_ADDRESS=peer1.org1.example.com:8051
peer chaincode query -C ${channelName} -n ${chaincodeName} -c '{"Args":["GetEntity","123456789"]}'


CORE_PEER_LOCALMSPID="Org2MSP" 
CORE_PEER_TLS_ROOTCERT_FILE=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/peers/peer0.org2.example.com/tls/ca.crt 
CORE_PEER_MSPCONFIGPATH=/opt/gopath/src/github.com/hyperledger/fabric/peer/crypto/peerOrganizations/org2.example.com/users/Admin@org2.example.com/msp
CORE_PEER_ADDRESS=peer0.org2.example.com:9051
peer chaincode query -C ${channelName} -n ${chaincodeName} -c '{"Args":["GetEntity","123456789"]}'
CORE_PEER_ADDRESS=peer1.org2.example.com:10051
peer chaincode query -C ${channelName} -n ${chaincodeName} -c '{"Args":["GetEntity","123456789"]}'
# for (( i = 0; i < 2; i++ )); do
#     CORE_PEER_ADDRESS=peer${i}.org2.example.com:7051
#     peer chaincode query -C ${channelName} -n ${chaincodeName} -c '{"Args":["GetEntity","b"]}'
# done

peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile  $ORDERER_CA  -C mychannel -n entity1cc3 -c '{"Args":["CreateEntity","abe","3","createTime"]}' --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles  $OrgA_CA --peerAddresses peer1.org1.example.com:8051 --tlsRootCertFiles  $OrgA_CA --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles  $OrgB_CA --peerAddresses peer1.org2.example.com:10051 --tlsRootCertFiles  $OrgB_CA 


peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile  $ORDERER_CA  -C mychannel -n entity1cc3 -c '{"Args":["CreateEntity","abc","3","createTime"]}' --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles  $OrgA_CA --peerAddresses peer1.org1.example.com:8051 --tlsRootCertFiles  $OrgA_CA --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles  $OrgB_CA --peerAddresses peer1.org2.example.com:10051 --tlsRootCertFiles  $OrgB_CA 

peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile  $ORDERER_CA  -C mychannel -n entity1cc3 -c '{"Args":["CreateEntity","abd","3","createTime"]}' --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles  $OrgA_CA --peerAddresses peer1.org1.example.com:8051 --tlsRootCertFiles  $OrgA_CA --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles  $OrgB_CA --peerAddresses peer1.org2.example.com:10051 --tlsRootCertFiles  $OrgB_CA 

peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile  $ORDERER_CA  -C mychannel -n entity1cc3 -c '{"Args":["CreateEntity","abf","3","createTime"]}' --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles  $OrgA_CA --peerAddresses peer1.org1.example.com:8051 --tlsRootCertFiles  $OrgA_CA --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles  $OrgB_CA --peerAddresses peer1.org2.example.com:10051 --tlsRootCertFiles  $OrgB_CA 



peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile  $ORDERER_CA  -C mychannel -n entity1cc3 -c '{"Args":["CreateEntity","ab1","3","createTime"]}' --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles  $OrgA_CA --peerAddresses peer1.org1.example.com:8051 --tlsRootCertFiles  $OrgA_CA --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles  $OrgB_CA --peerAddresses peer1.org2.example.com:10051 --tlsRootCertFiles  $OrgB_CA


peer chaincode invoke -o orderer.example.com:7050 --tls true --cafile  $ORDERER_CA  -C mychannel -n entity1cc3 -c '{"Args":["CreateEntity","ab2","3","createTime"]}' --peerAddresses peer0.org1.example.com:7051 --tlsRootCertFiles  $OrgA_CA --peerAddresses peer1.org1.example.com:8051 --tlsRootCertFiles  $OrgA_CA --peerAddresses peer0.org2.example.com:9051 --tlsRootCertFiles  $OrgB_CA --peerAddresses peer1.org2.example.com:10051 --tlsRootCertFiles  $OrgB_CA

peer chaincode query -C mychannel -n entity1cc3 -c '{"Args":["GetEntity","123456789"]}'


set -ex
peer chaincode query -C mychannel -n entity1cc3 -c '{"Args":["GetEntityByPartialCompositeKey","a", "123456789"]}'

peer chaincode query -C mychannel -n entity1cc3 -c '{"Args":["GetEntityByRange","a", "b"]}'



